//index.js
//获取应用实例
var app = getApp()

Page({
  data: {
    userName:null,
    members: [],     //页面渲染的数据 
    pageNum: 0,      //第几页/或者从第几条数据开始(根据后台接口的条件，我这里是第二种)
    isHideLoadMore: true,  //上拉的文字显示
    pageSize:8,
  },

  onLoad: function (options) {
    var that = this;
    wx.request({
      url: app.globalData.query_ip + '/wx_index',
      method: 'GET',
      data: {
        flag: "unlogin",
        pageNum: that.data.pageNum,    //第几页数据，每次+10（上拉加载方法中）
        pageSize: that.data.pageSize,
      },
      header: {
        'content-type': 'application/json'
      },
      success: function (res) {
        that.setData({
          members: res.data,
        })
        if (!res.data) {
          that.setData({
            sayloading: '没有更多数据了…^_^'
          })
        }
      },
    });
  },

  onShow:function(){
    var userName = app.globalData.userName;
    console.log('index userName===== ' + userName);
    if (userName) {
      this.setData({ userName: userName });
    }
  },

  show_details:function(e){
    var openid = app.globalData.openid;
    console.log('index openid===== ' + openid);
    if (openid) {
      var open_id = e.target.id;
      if (open_id){
        wx.navigateTo({
          url: '../info_details/info_details?open_id=' + open_id,
        })
      }
    }else{
      wx.navigateTo({
        url: '../login/login',
      })
    }
  },

  //下拉刷新
  onPullDownRefresh: function () {
    wx.showNavigationBarLoading() //在标题栏中显示加载
    setTimeout(function () {
      this.setData({
        pageNum: this.data.pageNum - this.data.pageSize,
        isHideLoadMore: true,
      })
      this.onLoad();
      wx.hideNavigationBarLoading() //完成停止加载
      wx.stopPullDownRefresh() //停止下拉刷新
    }, 1500);
  },

  //上拉加载
  onReachBottom: function () {
    wx.showNavigationBarLoading() //在标题栏中显示加载
    var that = this;
    setTimeout(function () {
      let pageNum = that.data.pageNum + that.data.pageSize;
      that.setData({
        pageNum: pageNum,
        isHideLoadMore: true,
      })
      that.onLoad();
      wx.hideLoading();// 隐藏加载框
      wx.hideNavigationBarLoading() //完成停止加载
    });
  },
})